import java.util.Arrays;

public class Leetcode838 {
    private static final char R = 'R';
    private static final char L = 'L';

    public String pushDominoes(String dominoes) {
        char[] domino = dominoes.toCharArray();
        int right = -1, left = 0;
        for (int i = 0; i < domino.length; i++) {
            char c = domino[i];
            if (c == R) {
                if (right != -1) {
                    Arrays.fill(domino, right, i, R);
                }
                right = i;
            } else if (c == L) {
                if (right == -1) {
                    Arrays.fill(domino, left, i, L);
                } else {
                    left = i;
                    while (right < left) {
                        domino[right++] = R;
                        domino[left--] = L;
                    }
                    right = -1;
                }
                left = i;
            }
        }
        if (right != -1) {
            Arrays.fill(domino, right, domino.length, R);
        }
        return String.valueOf(domino);
    }
}
